/**
 * Copyright (C) 2012 The SkyTvOS Project
 * <p/>
 * Version     Date           Author
 * ─────────────────────────────────────
 * 2014-6-26         luwei
 */

package com.tianci.appstore.ui.dialog;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager.NameNotFoundException;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.os.Message;
import android.view.Gravity;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout.LayoutParams;
import android.widget.TextView;

import com.coocaa.x.framework.utils.LogUtils;
import com.tianci.appstore.R;
import com.tianci.appstore.ui.SkinManager;
import com.tianci.appstore.ui.data.SkyListAppData;
import com.tianci.appstore.ui.widget.AlwaysMarqueeTextView;
import com.tianci.appstore.utils.SkyConstants;
import com.tianci.appstore.utils.SkyNetImageCache;
import com.tianci.appstore.utils.SkyNetImageCache.ImageLoadListener;

public abstract class CommonDialog extends BaseDialog implements ImageLoadListener {
    int contentWidth = SkyConstants.Div(955);
    int contentHeight = SkyConstants.Div(460);
    private FrameLayout contentLayout;
    protected ImageView iconImage;
    protected TextView mTitleView;
    protected LinearLayout mainLayout;
    protected ImageView bgImage;

    public CommonDialog(Context context) {
        super(context);
        initContentSize();
        initContentLayout();
        addBackgroundView();
        initTopLayout();
        addMainLayout();
        LogUtils.d("super after");
    }

    protected abstract void initContentSize();

    private void initContentLayout() {
        contentLayout = new FrameLayout(mContext);
        FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(contentWidth, contentHeight, Gravity.CENTER);
        params.bottomMargin = SkyConstants.iconWidth / 4;
        mLayout.addView(contentLayout, params);
    }

    private void addBackgroundView() {
        bgImage = new ImageView(mContext);
        bgImage.setId(123);
        SkinManager.setBackground(bgImage, R.drawable.bg_dialog);
        FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
                LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT,//contentHeight - SkyConstants.iconWidth / 2, 
                Gravity.BOTTOM);
        params.topMargin = SkyConstants.iconWidth / 2;
        contentLayout.addView(bgImage, params);
    }

    private void initTopLayout() {
        iconImage = new ImageView(mContext);
        mTitleView = new AlwaysMarqueeTextView(mContext);
        SkinManager.setBackground(mTitleView, R.drawable.title_bg);
        mTitleView.setTextSize(SkyConstants.Dpi(35));
        SkinManager.setTextColor(mTitleView, R.color.titleColor);
        mTitleView.setMaxWidth(SkyConstants.Div(690));
        mTitleView.setGravity(Gravity.CENTER_VERTICAL);
        mTitleView.setPadding(SkyConstants.Div(36), 0, SkyConstants.Div(39), SkyConstants.Div(9));
        LinearLayout.LayoutParams titleParams = new LinearLayout.LayoutParams(
                LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        titleParams.leftMargin = SkyConstants.Div(24);
        titleParams.topMargin = SkyConstants.Div(3);

        LinearLayout topLayout = new LinearLayout(mContext);
        topLayout.setPadding(SkyConstants.Div(36), 0, SkyConstants.Div(36), 0);
        topLayout.setGravity(Gravity.CENTER_VERTICAL);
        topLayout.addView(iconImage, new LayoutParams(SkyConstants.iconWidth, SkyConstants.iconWidth));
        topLayout.addView(mTitleView, titleParams);

        FrameLayout.LayoutParams topParams = new FrameLayout.LayoutParams(
                LayoutParams.WRAP_CONTENT, SkyConstants.iconWidth, Gravity.TOP);
        contentLayout.addView(topLayout, topParams);
    }

    private void addMainLayout() {
        mainLayout = new LinearLayout(mContext);
        int mainHeight = contentHeight - SkyConstants.iconWidth;
        if (mainHeight < 0)
            mainHeight = LayoutParams.MATCH_PARENT;
        FrameLayout.LayoutParams mainParams = new FrameLayout.LayoutParams(contentWidth,
                mainHeight, Gravity.BOTTOM);
        mainParams.topMargin = SkyConstants.iconWidth;
        contentLayout.addView(mainLayout, mainParams);
    }

    public void setIconTitle(Drawable iconDrawable, String title) {
        iconImage.setBackgroundDrawable(iconDrawable);
        mTitleView.setText(title);
    }

    public void setIconTitle(SkyListAppData data, String title) {
        mTitleView.setText(title);

        if (data == null)
            return;
        iconImage.setImageBitmap(null);
        if (data.getIconUrl() != null && !data.getIconUrl().equals("")) {
            if (SkyNetImageCache.getSkyNetImageCache().downloadAppListLogo(
                    data.getPkgName(), data.getIconUrl(), this, SkyConstants.iconWidth,
                    SkyConstants.iconWidth,0))
                iconImage.setBackgroundDrawable(null);
            else
                iconImage.setBackgroundDrawable(SkinManager.getResDrawable(R.drawable.icon_default));
        } else {
            try {
                iconImage.setBackgroundDrawable(SkinManager.getResDrawable(R.drawable.icon_default));
                PackageInfo info = mContext.getPackageManager().getPackageInfo(
                        ((SkyListAppData) data).getPkgName(), 0);
                iconImage.setBackgroundDrawable(info.applicationInfo.loadIcon(mContext
                        .getPackageManager()));
            } catch (NameNotFoundException e) {
                e.printStackTrace();
            }
        }
    }

    private Handler uiHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            // TODO Auto-generated method stub
            iconImage.setBackgroundDrawable(new BitmapDrawable((Bitmap) msg.obj));
            super.handleMessage(msg);
        }
    };

    @Override
    public void loadFinished(String url, Bitmap result) {
        // TODO Auto-generated method stub
        Message msg = uiHandler.obtainMessage();
        msg.obj = result;
        uiHandler.sendMessage(msg);
    }
}
